package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;

import com.google.gson.JsonObject;
import com.jiayen.cache.FileCacheManager;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.dialog.FirstLoginDialog;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.adapter.HelpAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.model.HelpModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.JpDiver;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 528787067@qq.com on 2017/6/21.
 */

public class HelpActivity extends SlidingActivity implements View.OnClickListener {

	@ViewInject(R.id.help_content_rv)
	private RecyclerView helpRv;
	@ViewInject(R.id.quick_help)
	private FrameLayout quickHelp;
	@ViewInject(R.id.feedback)
	private FrameLayout feedback;

	private List<HelpModel> helpModelList;
	private HelpAdapter helpAdapter;
	private String cachePath;
	private FileCacheManager cacheManager;
	private FirstLoginDialog helpDialog;

	@Override
	protected int getResourceId() {
		return R.layout.activity_help;
	}

	@Override
	protected void init() {
		topName.setText("帮助");
		rightIcon.setVisibility(View.GONE);
		leftIcon.setImageResource(R.mipmap.arrow_white_left);
		leftIcon.setOnClickListener(this);
		quickHelp.setOnClickListener(this);
		feedback.setOnClickListener(this);

		helpModelList = new ArrayList<>();
		helpAdapter = new HelpAdapter(mContext, helpModelList);
		helpRv.setHasFixedSize(true);
		helpRv.addItemDecoration(new JpDiver(mContext));
		helpRv.setLayoutManager(new LinearLayoutManager(mContext));
		helpRv.setAdapter(helpAdapter);
		helpAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
			@Override
			public void itemSelect(int position) {
				startActivity(new Intent(mContext, HelpCateListActivity.class)
						.putExtra("title", helpModelList.get(position).getCateTitle())
						.putExtra("cateId", helpModelList.get(position).getCateId()));
			}
		});

		cachePath = mContext.getCacheDir() + "/helpCate.dat";
		cacheManager = new FileCacheManager();

		readCache();
	}

	private void getData(){
		HttpHelper.getInstance(mContext).helpCate(new JsonObject(), 0x01, new ResultSubscriber.OnResultListener() {
			@Override
			public void onStart(int requestType) {}
			@Override
			public void onCompleted(int requestType) {}
			@Override
			public void onError(int requestType) {
				onCompleted(requestType);
			}
			@Override
			public void onNext(IModel data, int requestType) {
				if(data.getErrcode() != 0){
					ToasUtil.toast(mContext, data.getMessage());
					onError(requestType);
				} else {
					helpModelList.clear();
					List<HelpModel> helpModels = data.getList(HelpModel.class);
					if (helpModels != null && helpModels.size() > 0)
						helpModelList.addAll(helpModels);
					helpAdapter.updateAdapter(helpModelList);
					saveCache(cachePath, helpModelList);
				}
			}
		});
	}

	private void readCache(){
		Observable.create(new Observable.OnSubscribe<List<HelpModel>>() {
			@Override
			public void call(Subscriber<? super List<HelpModel>> subscriber) {
				List<HelpModel> appData = (List<HelpModel>) cacheManager.readObject(cachePath);
				subscriber.onNext(appData);
			}
		}).subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Subscriber<List<HelpModel>>() {
					@Override
					public void onCompleted() {
						getData();
					}
					@Override
					public void onError(Throwable e) {
						onCompleted();
					}
					@Override
					public void onNext(List<HelpModel> data) {
						if(data != null && data.size() > 0){
							helpModelList.clear();
							helpModelList.addAll(data);
							helpAdapter.updateAdapter(helpModelList);
						}
						onCompleted();
					}
				});
	}

	private void saveCache(final String cachePath, final Object data){
		new Thread(new Runnable() {
			@Override
			public void run() {
				cacheManager.wirteObject(data, cachePath);
			}
		}).start();
	}

	@Override
	public void onClick(View v) {
		if(v == leftIcon){
			onBackPressed();
		} else if(v == quickHelp){
			if(helpDialog == null) {
				helpDialog = new FirstLoginDialog(mContext);
				helpDialog.setDataGetListener(new FirstLoginDialog.DataGetListener() {
					@Override
					public void onStart() {
						loading.show();
					}
					@Override
					public void onFinish() {
						loading.cancel();
					}
				});
			}
			helpDialog.show();
		} else if(v == feedback){
			startActivity(new Intent(mContext, FeedbackActivity.class));
		}
	}
}
